/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.joolun.com
 * 注意：
 * 本软件为www.joolun.com开发研制，项目使用请保留此说明
 */
package com.jjsj.mall.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jjsj.mall.config.CommonConstants;
import com.jjsj.mall.entity.UserReceipt;
import com.jjsj.mall.mapper.UserReceiptMapper;
import com.jjsj.mall.service.UserReceiptService;
import org.springframework.stereotype.Service;

/**
 * 用户开票信息
 *
 * @author JL
 * @date 2019-09-11 14:28:59
 */
@Service
public class UserReceiptServiceImpl extends ServiceImpl<UserReceiptMapper, UserReceipt> implements UserReceiptService {

    @Override
    public boolean save(UserReceipt entity) {
        setIsDefault(entity);
        return super.save(entity);
    }

    @Override
    public boolean updateById(UserReceipt entity) {
        setIsDefault(entity);
        return super.updateById(entity);
    }

    /**
     * 修改默认开票
     *
     * @param entity
     */
    void setIsDefault(UserReceipt entity) {
        if (CommonConstants.YES.equals(entity.getIsDefault())) {
            UserReceipt userReceipt = new UserReceipt();
            userReceipt.setIsDefault(CommonConstants.NO);
            super.update(userReceipt, Wrappers.<UserReceipt>lambdaQuery()
                    .eq(UserReceipt::getUserId, entity.getUserId())
                    .eq(UserReceipt::getIsDefault, CommonConstants.YES));
        }
    }
}
